package com.sskj.one.proxy;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.one.R;
import com.sskj.one.bean.TeamLevelBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/05/22
 */
public class TeamLevelActivity extends BaseActivity<TeamLevelPresenter> {
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tv_today_commission)
    TextView tvTodayCommission;
    @BindView(R.id.tv_total_commission)
    TextView tvTotalCommission;
    @BindView(R.id.tv_team_num)
    TextView tvTeamNum;
    @BindView(R.id.tv_num_one)
    TextView tvNumOne;
    @BindView(R.id.tv_num_two)
    TextView tvNumTwo;
    @BindView(R.id.tv_num_three)
    TextView tvNumThree;
    @BindView(R.id.tv_num_four)
    TextView tvNumFour;
    @BindView(R.id.tv_num_five)
    TextView tvNumFive;
    @BindView(R.id.tv_num_six)
    TextView tvNumSix;
    @BindView(R.id.tv_num_seven)
    TextView tvNumSeven;
    @BindView(R.id.tv_see)
    TextView tv_see;
    @BindView(R.id.tv_unsee)
    TextView tv_unsee;
    @BindView(R.id.tv_more)
    TextView tv_more;
    @BindView(R.id.tv_today_commission_unsee)
    TextView tv_today_commission_unsee;
    @BindView(R.id.tv_total_commission_unsee)
    TextView tv_total_commission_unsee;
    @BindView(R.id.tv_team_num_unsee)
    TextView tv_team_num_unsee;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.view_one)
    View viewOne;
    @BindView(R.id.view_two)
    View viewTwo;
    @BindView(R.id.view_three)
    View viewThree;
    @BindView(R.id.view_four)
    View viewFour;
    @BindView(R.id.view_five)
    View viewFive;
    @BindView(R.id.view_six)
    View viewSix;
    @BindView(R.id.view_seven)
    View viewSeven;

    private SlimAdapter<TeamLevelBean.OrdersBean> slimAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_team_level;
    }

    @Override
    public TeamLevelPresenter getPresenter() {
        return new TeamLevelPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("团队分级");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.item_team_income, new SlimInjector<TeamLevelBean.OrdersBean>() {
            @Override
            public void onInject(TeamLevelBean.OrdersBean data, IViewInjector iViewInjector, List list) {
                iViewInjector
                        .text(R.id.tv_team, data.getLevel())
                        .text(R.id.tv_today_deal, data.getToday_num() + "单")
                        .text(R.id.tv_near_deal, data.getWeek_num() + "单")
                        .text(R.id.tv_today_commission, "$ " + NumberUtil.keep(data.getToday_assets(), 5))
                        .text(R.id.tv_near_commission, "$ " + NumberUtil.keep(data.getWeek_assets(), 5));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
    }

    @Override
    public void initData() {
        mPresenter.getTeamLevelInfo(true);
        smartRefreshView.setOnRefreshListener(refreshLayout -> mPresenter.getTeamLevelInfo(false));
        ClickUtil.click(300, viewOne, () -> DetailTeamActivity.start(TeamLevelActivity.this, "1"));
        ClickUtil.click(300, viewTwo, () -> DetailTeamActivity.start(TeamLevelActivity.this, "2"));
        ClickUtil.click(300, viewThree, () -> DetailTeamActivity.start(TeamLevelActivity.this, "3"));
        ClickUtil.click(300, viewFour, () -> DetailTeamActivity.start(TeamLevelActivity.this, "4"));
        ClickUtil.click(300, viewFive, () -> DetailTeamActivity.start(TeamLevelActivity.this, "5"));
        ClickUtil.click(300, viewSix, () -> DetailTeamActivity.start(TeamLevelActivity.this, "6"));
        ClickUtil.click(300, viewSeven, () -> DetailTeamActivity.start(TeamLevelActivity.this, "7"));
        ClickUtil.click(300, tv_see, () -> {
            tv_unsee.setVisibility(View.VISIBLE);
            tv_today_commission_unsee.setVisibility(View.VISIBLE);
            tv_total_commission_unsee.setVisibility(View.VISIBLE);
            tv_team_num_unsee.setVisibility(View.VISIBLE);
            tv_see.setVisibility(View.GONE);
            tvTodayCommission.setVisibility(View.INVISIBLE);
            tvTotalCommission.setVisibility(View.INVISIBLE);
            tvTeamNum.setVisibility(View.INVISIBLE);
        });
        ClickUtil.click(300, tv_unsee, () -> {
            tv_see.setVisibility(View.VISIBLE);
            tv_today_commission_unsee.setVisibility(View.INVISIBLE);
            tv_total_commission_unsee.setVisibility(View.INVISIBLE);
            tv_team_num_unsee.setVisibility(View.INVISIBLE);
            tv_unsee.setVisibility(View.GONE);
            tvTodayCommission.setVisibility(View.VISIBLE);
            tvTotalCommission.setVisibility(View.VISIBLE);
            tvTeamNum.setVisibility(View.VISIBLE);
        });
        //更多团队等级
        ClickUtil.click(300, tv_more, () -> {
            MoreLevelActivity.start(TeamLevelActivity.this);
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, TeamLevelActivity.class);
        context.startActivity(intent);
    }

    @SuppressLint("SetTextI18n")
    public void getTeamLevelInfoOK(TeamLevelBean data) {
        if (data == null) {
            return;
        }

        tv_more.setVisibility(data.getMore_dengji_display() == 0 ? View.GONE : View.VISIBLE);

        tvTodayCommission.setText("$ " + NumberUtil.keep(data.getToday_assets(), 5));
        tvTotalCommission.setText("$ " + NumberUtil.keep(data.getTotal_assets(), 5));
        tvTeamNum.setText(data.getTeam_num() + "人");
        if (data.getTeam() != null && data.getTeam().size() == 7) {
            tvNumOne.setText(data.getTeam().get(0).getNum() + "人");
            tvNumTwo.setText(data.getTeam().get(1).getNum() + "人");
            tvNumThree.setText(data.getTeam().get(2).getNum() + "人");
            tvNumFour.setText(data.getTeam().get(3).getNum() + "人");
            tvNumFive.setText(data.getTeam().get(4).getNum() + "人");
            tvNumSix.setText(data.getTeam().get(5).getNum() + "人");
            tvNumSeven.setText(data.getTeam().get(6).getNum() + "人");
        }
        if (data.getOrders() != null && slimAdapter != null) {
            slimAdapter.updateData(data.getOrders());
        }
    }

    public void stopRefresh() {
        if (smartRefreshView != null && smartRefreshView.isRefreshing()) {
            smartRefreshView.finishRefresh();
        }
    }

}
